package com.mioto.ld.module.product.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.product.model.ProductDetailVO;
import com.mioto.ld.module.product.model.ProductEditDTO;
import com.mioto.ld.module.product.model.ProductPagerVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.product.model.Product;
import com.mioto.ld.module.product.service.IProductService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:44
 */
@RestController
@RequestMapping("product")
@Api(tags = "产品信息管理")
public class ProductController {
    @Resource
    private IProductService productService;

    @GetMapping
    @ApiOperation(value="查询产品信息列表",response = Product.class)
    public ResultData list (HttpServletRequest request,Product product){
        return ResultData.success(productService.findList(product));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询产品信息",response = ProductPagerVO.class)
    public ResultData pager (HttpServletRequest request, Product product,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<ProductPagerVO> list = productService.findPager(product);
        PageInfo<ProductPagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/id/{id}")
    @ApiOperation(value="通过id查询产品信息详情",response = ProductDetailVO.class)
    public ResultData findById (@PathVariable("id")int id){
        return ResultData.success(productService.findById(id));
    }

    @PostMapping
    @ApiOperation(value="新增产品信息")
    public ResultData add (@RequestBody ProductEditDTO product){
        return Optional.of(productService.insert(product))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(product))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新产品信息 局部更新,忽略空值")
    public ResultData updateIgnoreNull (@RequestBody ProductEditDTO product){
        return Optional.of(productService.updateIgnoreNull(product))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(product))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/id/{id}")
    @ApiOperation(value="通过id删除产品信息")
    public ResultData deleteById (@PathVariable("id")int value){
        return Optional.of(productService.deleteById(value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除产品信息")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(productService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }
}